package com.lang8.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import com.lang8.util.database.ConnectionUtil;
import com.lang8.util.file.FileUtil;

public class LocaleUtil {
	public static void writeDataFile(){
		File datafile = new File(PathUtil.getWebInfFile(), "datafiles/languages.dat");
		if (!datafile.getParentFile().exists()) {
			datafile.getParentFile().mkdirs();
		}
		FileOutputStream fos = null;
		OutputStreamWriter osw = null;
		PrintWriter pw = null;
		
		try {
			fos = new FileOutputStream(datafile, false);
			osw = new OutputStreamWriter(fos, "UTF-8");
			pw = new PrintWriter(osw);
			writeLanguage(pw);
			pw.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} finally {
			FileUtil.closeFile(pw, osw, fos);
		}
	}
	private static void writeLanguage(PrintWriter pw) {
		Set<String> languageSet = new HashSet<String>();
		List<String[]> languageList = new ArrayList<String[]>();
		Locale[] locales = Locale.getAvailableLocales();
		for (Locale loc : locales) {
			String enLang = loc.getDisplayLanguage(Locale.US);
			if (languageSet.contains(enLang)) {
				continue;
			}
			languageSet.add(enLang);
			languageList.add(new String[]{enLang, loc.getDisplayLanguage(Locale.CHINA)});
		}
		Collections.sort(languageList, new Comparator<String[]>() {
			@Override
			public int compare(String[] o1, String[] o2) {
				return o1[0].compareTo(o2[0]);
			}
		});
		for (String[] langs : languageList) {
			pw.println(langs[0] + "," + langs[1]);
		}
	}
	private static void writeDatabase() {
		List<String[]> list = new ArrayList<String[]>();
		File file = new File(PathUtil.getWebInfFile(), "datafiles/languages.dat");
		FileInputStream fis = null;
		InputStreamReader isr = null;
		BufferedReader br = null;
		try {
			fis = new FileInputStream(file);
			isr = new InputStreamReader(fis, "UTF-8");
			br = new BufferedReader(isr);
			while (true) {
				String line = br.readLine();
				if (line == null) {
					break;
				}
				list.add(line.split(","));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			FileUtil.closeFile(br, isr, fis);
		}
		insertDB(list);
	}
	private static void insertDB(List<String[]> list) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			conn = ConnectionUtil.getConnection();
			conn.setAutoCommit(false);
			String sql = "INSERT INTO lang_info(LANG_KEY, DISP_LANG, LANG_VALUE) VALUES(?, ?, ?)";
			pstmt = conn.prepareStatement(sql);
			for (String[] strs : list) {
				pstmt.setString(1, strs[0]);
				pstmt.setString(2, "en_US");
				pstmt.setString(3, strs[0]);
				pstmt.executeUpdate();
			}
			for (String[] strs : list) {
				pstmt.setString(1, strs[0]);
				pstmt.setString(2, "zh_CN");
				pstmt.setString(3, strs[1]);
				pstmt.executeUpdate();
			}
			conn.commit();
		} catch (Exception e) {
			e.printStackTrace();
			ConnectionUtil.rollback(conn);
		} finally {
			ConnectionUtil.releaseConnection(null, pstmt, conn);
		}
	}
	public static void main(String[] args) {
//		writeDataFile();
//		writeDatabase();
		Locale l = Locale.CHINA;
		System.out.println(l.getDisplayLanguage());
	}
	
}
